﻿using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NetCoreBBS.Infrastructure;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using NetCoreBBS.ViewModels;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetCoreBBS.Entities;
using NetCoreBBS.Interfaces;
using System.Collections.Generic;

namespace NetCoreBBS.Controllers
{
    public class HomeController : Controller
    {
        private ITopicRepository _topic;
        private IRepository<TopicNode> _node;
        public UserManager<ApplicationUser> UserManager { get; }
        public HomeController(ITopicRepository topic, IRepository<TopicNode> node, UserManager<ApplicationUser> userManager)
        {
            _topic = topic;
            _node = node;
            UserManager = userManager;
        }
        public IActionResult Index([FromServices]IUserServices user)
        {
            //var pagesize = 20;
            //var pageindex = 1;
            //Page<Topic> result = null ;
            //if (!string.IsNullOrEmpty(Request.Query["page"]))
            //    pageindex = Convert.ToInt32(Request.Query["page"]);
            //if (!string.IsNullOrEmpty(Request.Query["s"]))
            //    result = _topic.PageList(r => r.Title.Contains(Request.Query["s"]), pagesize, pageindex);
            //else
            //    result = _topic.PageList(pagesize, pageindex);
            //ViewBag.Topics = result.List.Select(r=>new TopicViewModel
            //{
            //    Id = r.Id,
            //    NodeId = r.Node.Id,
            //    NodeName = r.Node.Name,
            //    UserName = r.User.UserName,
            //    Avatar=r.User.Avatar,
            //    Title = r.Title,
            //    Top = r.Top,
            //    Type=r.Type,
            //    ReplyCount = r.ReplyCount,
            //    LastReplyTime = r.LastReplyTime,
            //    CreateOn = r.CreateTime
            //}).ToList();
            //ViewBag.PageIndex = pageindex;
            //ViewBag.PageCount = result.GetPageCount();
            
            var announcementList = _topic.TableNoTracking.Where(x => x.NodeId == 1).OrderByDescending(x => x.CreateTime).Take(5);
            ViewBag.AnnouncementList = announcementList;
            ViewBag.User = user.User.Result;
            var nodes = _node.List().ToList();
            var topicNodes = new List<TopicNodeViewModel>();
            foreach(var n in nodes)
            {
                var tn = TopicNodeViewModel.ToModel(n);
                tn.TopicCount = _topic.Count(x => x.NodeId == n.Id);
                topicNodes.Add(tn);
            }
            ViewBag.Nodes = topicNodes;
            return View();
        }

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public IActionResult Index(Topic topic)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        topic.CreateOn = DateTime.Now;
        //        topic.Type = TopicType.Normal;
        //        _topic.Add(topic);
        //    }
        //    return RedirectToAction("Index");
        //}

        public IActionResult About()
        {
            ViewData["Message"] = "安全管理平台";
            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }
}
